package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivity;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivityMgr;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_TRADE_WAR_PUBLISH_WANTED_TO_WANTED_RESP, desc = "跨服商战发布通缉响应")
public class CrossTradeWarPublishWantedToWantedRespCmd extends CrossUnionCommand {
    private int activityId;
    private long userId;
    private UserBaseInfo userBaseInfo;
    private long wantedUserId;
    private UserBaseInfo wantedBaseInfo;
    private int useItemId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CrossTradeWarPublishWantedToWantedRespMsg reqMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToWantedRespMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserBaseData());
        useItemId = reqMsg.getUseItemId();
        ret = reqMsg.getRet();
        if(0 == ret) {
            wantedUserId = reqMsg.getWantedUserId();
            wantedBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getWantedBaseData());
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(0 == ret) {
            ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfo(activityId);
            if (null == activityInfo) {
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }
            int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
            CrossTradeWarActivity activity = CrossTradeWarMgr.getActivity(activityId, groupId);
            if (activity == null){
                ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
                return;
            }

            ret = activity.publishWanted(activityId, userId, userBaseInfo, wantedUserId, wantedBaseInfo, useItemId);
        }
    }

    @Override
    public void sendData() {
        if(0 != ret) {
            CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setActivityId(activityId);
            respMsg.setUseItemId(useItemId);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PUBLISH_WANTED_RESP, respMsg);
            MessageHelper.sendPacket(userBaseInfo.getServerId(), userId, pbMsg);
        }
    }
}
